<script setup lang='ts'>
  import { onMounted, reactive, ref } from 'vue';
  import { financeApi } from '@/api/controller';
  import { Toastloading } from '@/utils/public';
  import { getPlayEndTime, getDateArray } from '@/utils'
  import xmDatePicker from '@/components/xmDatePicker/index.vue';
  import Pagination from "@/components/Pagination/index.vue";
  import deductForm from './deductForm.vue';
  import serachUserid from '@/components/serachUserid/index.vue';
  const deductFormRef = ref()
  const action = reactive({
    total:0,
    user_id:'',
    uuid:'',
    list:[] as any[],
    timeObj:getPlayEndTime(getDateArray('ymdhis'))
  })
  const listQuery = reactive({
    page:1,
    limit:20,
  })
  const handleSerach = ()=>{
    listQuery.page = 1
    getList()
  }
  const handleDeduct = ()=>{
    deductFormRef.value.openDialog()
  }
  const getList = async()=> {
    const loading = Toastloading()
    const { playTime, endTime } = action.timeObj
    const {uuid} = action
    const res = await financeApi.fetchList({
      begin_date:playTime,
      end_date:endTime,
      uuid,
      ...listQuery
    })
    loading.close()
    const { data=[], total=0 } = res.data
    action.total = total
    action.list = data
  }
  onMounted(()=>{
    getList()
  })
</script>

<template>
  <div class="xm-content">
    <div class="xm-headTab">
      <div class="labelBox">
        <xmDatePicker type="daterange" v-model:timeval="action.timeObj"  style="margin-right: 0;"/>
        <div style="width:150px;margin-right: 2em;">
          <serachUserid v-model:user_id="action.user_id" v-model:uuid="action.uuid"/>
        </div>
        <div class="labelItem">
          <el-button type="primary" @click="handleSerach">查询</el-button>
          <el-button type="primary" @click="handleDeduct">扣除金币</el-button>
        </div>
      </div>
    </div>
    <el-table
      class="xm-main xm-TableCenter"
      :data="action.list"
      border
    >
      <el-table-column width="220" prop="created_at" label="日期" />
      <el-table-column width="100" prop="user_id" label="用户ID" />
      <el-table-column width="100" prop="uuid" label="用户UUID" />
      <el-table-column min-width="100" show-overflow-tooltip prop="nickname" label="用户昵称" />
      <el-table-column width="120" prop="kk_value" label="变动金币" />
      <el-table-column width="120" prop="kk_balance" label="金币余额" />
      <el-table-column width="220" show-overflow-tooltip prop="desc" label="描述" />
    </el-table>
    <Pagination v-show="action.total > 0" :total="action.total" v-model:page="listQuery.page"
      v-model:limit="listQuery.limit" @pagination="getList" />
    <deductForm ref="deductFormRef" @update-list="getList" />
  </div>
</template>
